<?php
$loader->requireOnce('includes/RadException.class.php');
$loader->requireOnce('includes/RadQueue/RadQueueFactory.class.php');
$loader->requireOnce('includes/RadQueue/ARadQueue.class.php');
class RadQueueProcessor {
	/**
	 * Add a radiology ticket to radiology queue
	 *
	 * @param int $radTicketId Id of radiology ticket
	 * @param int $roomId Room Id
	 * @param string $queueType Queue type
	 */
    public static function addTicketToQueue($radTicketId, $roomId, $queueType) {
    	// Check whether the rad_ticket exists or not
        $objRadTicket   =   ORDataObject::factory('RadTicket');
        if ($objRadTicket->checkTicketExist($radTicketId) === false) {
            throw new RadException('The radiology ticket with id=' . $radTicketId . " does not exist", RadErrorCode::TICKET_NOT_EXIST);
        }
        $objQueue   =   RadQueueFactory::createQueue($queueType);
        $objQueue->addItem($radTicketId, $roomId);
    }

    /**
     * Add a radiology ticket to waiting queue. This method is mainly used by HIS.
     *
     * @param int $radTicketId Id of radiology ticket
     * @param unknown_type $roomId Room Id
     */
    public static function addTicketToWaitingQueue($data) {
    	$radTicketId   =   $data['radTicketId'];
    	$roomId        =   $data['roomId'];
        $queueType  =   ARadQueue::QUEUE_TYPE_WAITING;
        self::addTicketToQueue($radTicketId, $roomId, $queueType);
    }

    public static function renderQueue($roomId, $enumPatientType) {
    	$result['waiting']     =   '';
    	$result['in_progress'] =   '';
        $objQueue   =   RadQueueFactory::createQueue(ARadQueue::QUEUE_TYPE_WAITING);
        $result['waiting']  =   $objQueue->renderQueue($roomId, $enumPatientType);
        $objQueue   =   RadQueueFactory::createQueue(ARadQueue::QUEUE_TYPE_INPROGRESS);
        $result['in_progress']  =   $objQueue->renderQueue($roomId, $enumPatientType);
        return $result;
    }

    public static function moveQueueItem($radTicketId, $destQueue)  {
    	$objRadQueue    =   ORDataObject::factory('RadQueue')->getObjectByTicket($radTicketId);
        $queueId        =   EnforceType::int($objRadQueue->get_id());
        if( $queueId <= 0) {
            throw new RadException('The radiology queue item with radTicketId=' . $radTicketId . " does not exist", RadErrorCode::QUEUE_ITEM_NOT_EXIST);
        }
        // Add radiology ticket to RadWaiting queue
        self::addTicketToQueue($radTicketId, $objRadQueue->getRoomId(), $destQueue);
    }

    public function checkQueueItemExist($radTicketId, $queueType) {
        $objQueue   =   RadQueueFactory::createQueue($queueType);
        return $objQueue->checkQueueItemExist($radTicketId);
    }
}
?>